<?php
defined('SYSPATH') OR die('No direct access allowed.');
/**
 * lowongan.php
 * @author ranuandra
 *
 * 30 Apr 2012 22:44:05
 *
 * 
 */

class Controller_Lowongan extends Kohana_Nasmoco
{
	
	public function action_index()
	{
		//$pelamar = ORM::factory('mstpelamar')->where('user_id', '=', Auth_ORM::instance()->get_user()->id)->find();
		//$datas = $pelamar->pelamarlowongan->find_all();
		$datas = ORM::factory('mstlowongan')->where('DATE("publisheddate")', '<=', date('Y-m-d'))->where('DATE("dateto")', '>=', date('Y-m-d'))->find_all();
		$view = View::factory('lowongan/list');
		$view->datas = $datas;
		$this->template->container = $view;
	}
	
	public function action_proses()
	{
		$id = Request::current()->param('id');
		if($id != "")
		{
			$pelamar = ORM::factory('pelamarlowongan', $id);
			$datas = $pelamar->pelamarproses->find_all();
			$view = View::factory('lowongan/proses');
			$view->pelamar = $pelamar;
			$view->datas = $datas;
			$this->template->container = $view;			
		}
	}
	
	
	public function action_detail()
	{

		$id = Request::current()->param('id');
		$data = ORM::factory('mstlowongan', $id);
		$view = View::factory('lowongan/index');
		if(Auth_ORM::instance()->logged_in())
		{
			$cek = $data->pelamarlowongan->where('mstpelamar_id', '=', Auth_ORM::instance()->get_user()->mstpelamar->id)->find_all();
			//print_r($cek);
			if(count($cek) > 0)
			{
				$view->message = "Anda sudah mengajukan lamaran untuk kualifikasi lamaran ini";
				$view->next = false;
			}
			else 
			{
				$view->next = true;
			}
			
			$view->cek_data = $this->cek_data();
			
			echo $this->cek_data();
		}
		$datas = ORM::factory('mstlowongan')->where('DATE("publisheddate")', '<=', date('Y-m-d'))->where('DATE("dateto")', '>=', date('Y-m-d'))->find_all();		
		$view->data = $data;
		$view->datas = $datas;
		$this->template->container = $view;
	}
	
	public function action_confirm()
	{
		$id = Request::current()->param('id');
		$data = ORM::factory('mstlowongan', $id);
		$view = View::factory('lowongan/confirm');
		$cek = $data->pelamarlowongan->where('mstpelamar_id', '=', Auth_ORM::instance()->get_user()->id)->find();
		if(isset($cek->kode_lamaran))
		{
			$view->message = "Anda sudah mengajukan lamaran untuk kualifikasi lamaran ini";
		}
		else 
		{
			$view->message = "Apakah anda benar-benar akan mengajukan lamaran dengan kualifikasi di bawah ini:";
		}
		
		$view->captcha = Captcha::instance();
		$view->data = $data;
		$this->template->container = $view;	
		
	}
	
	public function action_save()
	{
		if($_POST)
		{
			if(Captcha::valid(Request::current()->post('captcha')))
			{
				$year = date("Y");
				$month = date("m");
				$mx = DB::select(array('MAX("id")', 'maks'))->from('pelamarlowongans')->execute();
	
				$no = $mx[0]['maks'] + 1;
				$lowongan = ORM::factory('pelamarlowongan');
				$lowongan->mstpelamar_id = Auth_ORM::instance()->get_user()->mstpelamar->id;
				$lowongan->mstlowongan_id = Request::current()->post('mstlowongan_id');
				$lowongan->tgl_apply = date("Y-m-d");
				$lowongan->kode_lamaran = "USR-".$year.$month."-".$no;
				$lowongan->save();
				
				$view = View::factory('lowongan/sukses');
				$this->template->container = $view;
			}
			else 
			{
				$id = Request::current()->post('mstlowongan_id');
				$data = ORM::factory('mstlowongan', $id);
				$view = View::factory('lowongan/confirm');
				$cek = $data->pelamarlowongan->where('mstpelamar_id', '=', Auth_ORM::instance()->get_user()->id)->find();
				if(isset($cek->kode_lamaran))
				{
					$view->message = "Anda sudah mengajukan lamaran untuk kualifikasi lamaran ini";
				}
				else 
				{
					$view->message = "Apakah anda benar-benar akan mengajukan lamaran dengan kualifikasi di bawah ini:";
				}
				$errors = array();
				$errors['captcha'] = 'Kode tidak sesuai';
				$view->captcha = Captcha::instance();
				$view->errors = $errors;
				$view->data = $data;
				$this->template->container = $view;					
			}
			
		}
	}
}